-- This file is hereby placed in the Public Domain
local addonName, ns = ...
local addon = ns[addonName]
local oUF = ns.oUF

local utils = addon.utils
local tags = addon.tags
local cfg = addon.cfg
local units = 'party'

local function PartyPetStyle(self, unit)
   local fg = self.bg:CreateTexture(nil, 'ARTWORK')
   fg:SetWidth(256*.7)
   fg:SetHeight(64*.7)
   fg:SetTexture(utils.mediaPath.."miniframe")
   fg:SetPoint('CENTER', self, 0, 0)
   self.mini = true
   self:SetSize(75, 22)

   self.Health = CreateFrame('StatusBar', nil, self)
   self.Health:SetParent(self)
   self.Health:SetPoint"CENTER"
   self.Health:SetFrameStrata("LOW")
   self.Health:SetFrameLevel(3)
   self.Health:SetStatusBarTexture(utils.mediaPath.."normTex")
   
   self.Health:SetStatusBarColor(0.2, 0.2, 0.2)
   self.Health:SetHeight( self:GetHeight() * .8 )
   self.Health.frequentUpdates = true
   self.Health.colorHappiness = unit == 'pet' and true
   self.Health.colorClass = true
   self.Health.Smooth = true
   self.Health.colorClass = true   
   self.Health:SetPoint("TOPRIGHT", self,"TOPRIGHT", -8, -4) 
   self.Health:SetPoint("BOTTOMLEFT", self,"BOTTOMLEFT", 8, 6)

   local info = self:CreateFontString(nil, 'OVERLAY')
   info:SetFont(STANDARD_TEXT_FONT, 12, 'OUTLINE')
   info:SetPoint('CENTER', 0, 0)
   info:SetJustifyH('RIGHT')
   self:Tag(info, '[smartINFO][shortlarge]')
end

addon:addLayoutElement('partypet', PartyPetStyle)
addon:setSize('partypet', 50, 20)

local function PartyTargetStyle(self, unit)
   local tex = self:CreateTexture(nil, 'ARTWORK')
   tex:SetTexture(utils.mediaPath .. 'smallframe')
   tex:SetPoint('CENTER', self, -1, 0)
   tex:SetTexCoord(1,0,0,1)
   utils.setSize(tex, 256 * .8, 128 * .8)
   self.texture = tex

   local bg = self.bg:CreateTexture(nil, 'BACKGROUND')
   bg:SetTexture(utils.mediaPath .. 'smallframeBG')
   bg:SetPoint('CENTER', self, -1, 0)
   bg:SetTexCoord(1,0,0,1)
   utils.setSize(bg, 256 * .8, 128 * .8)
   self.bg_tex = bg

   self.Health = CreateFrame('StatusBar', nil, self)
   self.Health:SetParent(self)
   self.Health:SetPoint"CENTER"
   self.Health:SetFrameStrata("LOW")
   self.Health:SetFrameLevel(3)
   self.Health:SetStatusBarTexture(utils.mediaPath.."normTex")
   
   self.Health:SetStatusBarColor(0.2, 0.2, 0.2)
   self.Health:SetHeight( self:GetHeight() * .8 )
   self.Health.frequentUpdates = true
   self.Health.colorHappiness = unit == 'pet' and true
   self.Health.colorClass = true
   self.Health.Smooth = true
   self.Health.colorClass = true   

   self.Health:SetPoint("TOPLEFT", self, "TOPLEFT", 10, -25) 
   self.Health:SetPoint("BOTTOMRIGHT", self, "BOTTOMLEFT", 75, 20)

   local info = self:CreateFontString(nil, 'OVERLAY')
   info:SetFont(STANDARD_TEXT_FONT, 12, 'OUTLINE')
   info:SetPoint('CENTER', -20, 0)
   info:SetJustifyH('RIGHT')
   self:Tag(info, '[smartINFO][shortlarge]')

   local lvl = self:CreateFontString(nil, 'OVERLAY')
   lvl:SetFont(STANDARD_TEXT_FONT, 12, 'OUTLINE')
   lvl:SetPoint('RIGHT', -38, -14)
   self:Tag(lvl, '[diffcolor][level] [shortclassification]')

   local portrait = CreateFrame('PlayerModel', nil, self)
   portrait:SetFrameStrata('BACKGROUND')
   --    portrait:SetFrameLevel(1)
   portrait:SetHeight(30)
   portrait:SetWidth(40)
   portrait:SetPoint('BOTTOMRIGHT', self,  -3, 18)
   self.Portrait = portrait

   local leader = self:CreateTexture(nil, 'OVERLAY')
   self.Leader = leader
   leader:SetSize(18, 18)
   leader:SetTexture(utils.mediaPath .. 'Leader')
   leader:SetPoint('TOPRIGHT', self, -18, 5)
end

addon:addLayoutElement('partytarget', PartyTargetStyle)
addon:setSize('partytarget', 120, 33)

local function PartyStyle(self, unit)
   ns.style_party(self, unit)

   local hp = self:CreateFontString(nil, 'OVERLAY')
   hp:SetFont(STANDARD_TEXT_FONT, 12, 'OUTLINE')
   hp:SetPoint('RIGHT', self.Health, -2, -1)
   hp:SetJustifyH('LEFT')
   self:Tag(hp, tags.smartHP)

   local mp = self:CreateFontString(nil, 'OVERLAY')
   mp:SetFont(STANDARD_TEXT_FONT, 12, 'OUTLINE')
   mp:SetPoint('LEFT', self.Health, 5, -1)
   self:Tag(mp, tags.smartMP)

   local lvl = self:CreateFontString(nil, 'OVERLAY')
   lvl:SetPoint('TOP', self, 'BOTTOM', -58, 16)
   lvl:SetFont(STANDARD_TEXT_FONT, 12, 'OUTLINE')
   self:Tag(lvl, '[diffcolor][level] [shortclassification]')--tags.lvl)

   local name = self:CreateFontString(nil, 'OVERLAY')
   name:SetPoint('TOP', self, 'BOTTOM', 10, 32)
   name:SetFont(STANDARD_TEXT_FONT, 12, 'OUTLINE')
   self:Tag(name, tags.majorname)

   local leader = self:CreateTexture(nil, 'OVERLAY')
   self.Leader = leader
   leader:SetSize(18, 18)
   leader:SetTexture(utils.mediaPath .. 'Leader')
   leader:SetPoint('TOPLEFT', self, -20, -10)

   self.RaidIcon = self:CreateTexture(nil, 'OVERLAY')
   self.RaidIcon:SetHeight(10)
   self.RaidIcon:SetWidth(10)
   self.RaidIcon:SetPoint('TOP', name, 0, 10)
end

addon:addLayoutElement(units, PartyStyle)
addon:setSize(units, 125, 65)

addon:spawn(
   function()
      local u = addon.units
      local party = oUF:SpawnHeader(
	 addonName .. '_PartyFrame', nil, 'solo,party,raid',
	 'oUF-initialConfigFunction', [[self:SetWidth(125); self:SetHeight(65)
]],
	 'showParty', true,
	 'unitsPerColumn', 1,
	 'maxColumns', 4,
	 'point', 'LEFT',
	 "yOffset", -cfg.party.Yspacing,
	 'columnSpacing', 20,
	 'columnAnchorPoint', 'TOP',
	 'template', 'add_party'
      )
	 party:SetPoint("TOPLEFT", UIParent, "LEFT", cfg.party.Xnormal, cfg.party.Ynormal)
	 party:Show()

	 u.party = party
      end
   )


